<template>
    <div class="app-container falseTabel-container">
        <div class="customContainer ">
            <!-- 时间-品牌-门店-区域-机台类型-机台名称-搜索 -->
            <dj-search-tbsatn ref="search" @fetchData="fetchData" categoryId="298"></dj-search-tbsatn>
            <el-table highlight-current-row show-summary :data="list" v-loading.body="listLoading" border
                :summary-method="getSummaries" size="small">
                <el-table-column align="center" prop="datetime" label="日期" sortable></el-table-column>
                <el-table-column align="center" prop="no" label="机台编号" sortable></el-table-column>
                <el-table-column align="center" prop="char_project_type" label="机台名称" sortable>
                    <template slot-scope="scope">
                        {{scope.row.project_type}}
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="char_project_type_first" label="机台类型" sortable>
                    <template slot-scope="scope">
                        {{scope.row.project_type_first}}
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="push_coins" label="总收币数" sortable
                    :render-header="(h,obj) => renderHeader(h,obj,'刷卡扣币+扫码上币')">
                </el-table-column>
                <el-table-column align="center" prop="push_money" label="收币金额" sortable
                    :render-header="(h,obj) => renderHeader(h,obj,'收币金额=机台收币总数x当前币值')"></el-table-column>
                <el-table-column align="center" prop="sweep_money" label="扫码支付金额" sortable></el-table-column>
                <el-table-column align="center" prop="times" label="游玩次数" sortable
                    :render-header="(h,obj) => renderHeader(h,obj,'扫码和刷卡游玩的总局')"></el-table-column>
                <el-table-column align="center" prop="gift_cost" label="出货成本" sortable
                    :render-header="(h,obj) => renderHeader(h,obj,'出货成本=出货数*成本单价')"></el-table-column>
                <el-table-column align="center" prop="profit" label="毛利" sortable
                    :render-header="(h,obj) => renderHeader(h,obj,'毛利=收币金额+扫码支付金额-出货成本(单机台出多款货为所有出货成本总数）')">
                </el-table-column>
                <el-table-column align="center" prop="grossprofit" label="毛利率" sortable
                    :render-header="(h,obj) => renderHeader(h,obj,'毛利率=毛利/(收币金额+扫码支付金额付)')">
                    <template slot-scope="scope">
                        {{scope.row.grossprofit+'%'}}
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
</template>

<script>
    import {
        getList,
    } from "@/api/storage_control/prize_machine_gain_form";
    import { renderHeader } from '@/utils';
    export default {
        data() {
            return {
                renderHeader,
                listLoading: false,
                list: [],
                total: {}
            };
        },
        methods: {
            getSummaries(param) {
                const { columns, data } = param;
                const sums = [];
                columns.forEach((column, index) => {
                    if (index === 0) {
                        sums[index] = '合计';
                        return;
                    }
                    if (index === 1) {
                        sums[index] = '-';
                        return;
                    }
                    if (index === 2) {
                        sums[index] = '-';
                        return;
                    }
                    if (index === 10) {
                        sums[index] = (this.total.all_push_money * 1 + this.total.all_sweep_money * 1 == 0) ? '-' : ((this.total.all_profit) / (this.total.all_push_money * 1 + this.total.all_sweep_money * 1) * 100).toFixed(2) * 1 + '%'
                        return;
                    }
                    const values = data.map(item => Number(item[column.property]));
                    if (index > 0) {
                        if (!values.every(value => isNaN(value))) {
                            sums[index] = values.reduce((prev, curr) => {
                                const value = Number(curr);
                                if (!isNaN(value)) {
                                    return prev + curr;
                                } else {
                                    return prev;
                                }
                            }, 0);
                            sums[index] = (sums[index] * 1).toFixed(2) * 1;
                        } else {
                            sums[index] = '-';
                        }
                    }
                });
                return sums;
            },
            // 列表获取
            fetchData(formInline) {
                this.listLoading = true;
                const that = this
                let totalData = {
                    all_unitsshipped: 0,
                    all_unitsshipped_money: 0,
                    all_profit: 0,
                    all_push_money: 0,
                    all_sweep_money: 0,
                };
                getList(formInline).then(response => {
                    response.forEach(function (val, i) {
                        val.no *= 1;
                        val.push_coins *= 1;
                        val.push_money = (val.push_money / 1000000).toFixed(2) * 1;
                        val.sweep_count *= 1;
                        val.sweep_money = (val.sweep_money / 1000000).toFixed(2) * 1;
                        val.times = val.push_count * 1 + val.sweep_count * 1;
                        val.gift_cost = (val.gift_cost / 1000000).toFixed(2) * 1;
                        val.profit = (val.push_money * 1 + val.sweep_money * 1 - val.gift_cost * 1).toFixed(2) * 1
                        val.grossprofit = (val.profit / (val.push_money * 1 + val.sweep_money * 1) * 100).toFixed(2) * 1
                        totalData.all_push_money += val.push_money;
                        totalData.all_sweep_money += val.sweep_money;
                        totalData.all_profit += val.profit;
                    });
                    totalData.all_push_money = totalData.all_push_money.toFixed(2) * 1;
                    totalData.all_sweep_money = totalData.all_sweep_money.toFixed(2) * 1;
                    totalData.all_profit = totalData.all_profit.toFixed(2) * 1;
                    this.list = response;
                    this.total = totalData
                    console.log(this.total.all_profit, '----', this.total.all_push_money, '----------', this.total.all_sweep_money)
                    this.listLoading = false;
                }).catch(res=>{
                    this.listLoading=false;
                })
            },
        }
    };
</script>